import request from "@/axios/request";

const {ElMessage} = require("element-plus");

export default {
    name: "TenantHome",
    data() {
        return {
            loading: true,
            landlordId: null,
            username: "",
            password: "",
            phone: "",
            email: "",
            images: [
                { src: require('@/assets/landlord_banner1.png'), alt: 'pic1' },
                { src: require('@/assets/landlord_banner2.png'), alt: 'pic2' },
                { src: require('@/assets/landlord_banner4.png'), alt: 'pic3' },
                { src: require('@/assets/landlord_banner3.png'), alt: 'pic4' }
            ],
            total: 0,
            allData: [], 
            firstSixData: [],
            // 状态映射
            statusMap: {
                rented: { text: '已租', type: 'info' },
                available: { text: '待租', type: 'success' },
                pending: { text: '审核中', type: 'warning' },
                rejected: { text: '审核未通过', type: 'danger' }
            }
        };
    },
    computed: {
        paginatedData() {
            const start = 0;
            const end = 6;
            return this.allData.slice(start, end);
        }
    },
    created() {
        this.loadMyInfo();
        this.load();
        this.loading = true;
        setTimeout(() => {
            this.loading = false;
        }, 1000);
    },
    methods: {
        async load() {
            try{
                const res = await request.get("/house/findAll");
                if (res.code === 200) {
                    // 保留原始状态码，新增 displayStatus 字段用于显示
                    this.allData = (res.data || []).map(item => ({
                        ...item,
                        displayStatus: this.statusMap[item.status]?.text || '未知状态'
                    }));
                    this.total = this.allData.length;
                    this.firstSixData = this.paginatedData;
                } else {
                    ElMessage.error(res.msg || "加载失败");
                    this.allData = [];
                    this.firstSixData = [];
                    this.total = 0;
                }
                this.loading = false;
            } catch (err){
                console.error(err);
                ElMessage.error("网络错误，请稍后再试");
                this.allData = [];
                this.firstSixData = [];
                this.total = 0; 
                this.loading = false;
            }
        },
        loadMyInfo() {
            const token = JSON.parse(window.sessionStorage.getItem('userInfo'));

            if(!token) {
                ElMessage.error("获取用户信息失败");
                console.log("获取用户信息失败");
                return;
            }

            this.TenantId = token.TenantId;
            this.username = token.username;
            this.password = token.password;
            this.phone = token.phone;
            this.email = token.email;
        },
        SignOut() {
            sessionStorage.clear()
            ElMessage({
                message: '用户退出登录',
                type: 'success',
            });
            this.$router.replace({path: '/Login'});
        },
        // 状态标签类型获取方法
        getStatusTagType(status) {
            return this.statusMap[status]?.type || '';
        },
        // 状态标签文本获取方法
        getStatusLabel(status) {
            return this.statusMap[status]?.text || '未知状态';
        }
    },
};